package com.fan.teamer.process;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.model.domain.TeamMember;
import com.fan.teamer.model.domain.TeamMemberId;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamMemberService;
import com.fan.teamer.service.TeamService;

public class AddMemberToTeamProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("AddMemberToTeamProcess");

	private Member member;
	private Team team;
	private String memberRole;
	private TeamMemberService teamMemberService;
	private TeamService teamService;
	private MemberService memberService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		AddMemberToTeamProcess.logger = logger;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public String getMemberRole() {
		return memberRole;
	}

	public void setMemberRole(String memberRole) {
		this.memberRole = memberRole;
	}

	public TeamMemberService getTeamMemberService() {
		return teamMemberService;
	}

	public void setTeamMemberService(TeamMemberService teamMemberService) {
		this.teamMemberService = teamMemberService;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public int initCheck() {
		if (teamService == null || teamMemberService == null
				|| memberService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (team == null || member == null) {
			setErrorMessage("Domain cannot be null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!teamService.hasTeam(team.getId())) {
			setErrorMessage("Team: " + team.getName()
					+ " doesn't exist in Country: " + team.getCountry());
			return TeamerGlobalVariables.FAILED;
		}

		if (!memberService.hasMember(member.getEmail())) {
			setErrorMessage("Member: " + member.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}

		if (teamService.hasMember(team, member)) {
			setErrorMessage("Member: " + member.getEmail()
					+ " already exists in Team " + team.getName() + " "
					+ team.getCountry());
			return TeamerGlobalVariables.FAILED;
		}
		return TeamerGlobalVariables.SUCCESS;
	}

	@Override
	public Integer processImpl() {

		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}
		
		TeamMember teamMember = new TeamMember();
		TeamMemberId teamMemberId = new TeamMemberId();
		teamMemberId.setMemberEmail(member.getEmail());
		teamMemberId.setTeamId(team.getId());

		teamMember.setId(teamMemberId);

		// any one who joined team will be participant at beginning
		memberRole = TeamerGlobalVariables.PARTICIPANT;
		teamMember.setMemberRole(memberRole);

		try {
			teamMemberService.addTeamMember(teamMember);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Member: " + member.getEmail()
					+ " cannot be added to team: " + team.getName());
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
